<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 资源设置 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-system"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 新增系统 -->
		<div class="widget radius-bordered">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 系统信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-system">
				<!-- dynamic data -->
			</div>
		</div>
		
		<!-- TODO 模块列表 -->
		<div class="widget radius-bordered " id="editor-panel-module">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 模块列表 </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" data-toggle="tooltip" data-placement="top" onclick="handlerAddRow('#table-module')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor" id="tables-form-module">
					<table data-ajax="handlerSearch" 
								 data-method="post" 
								 data-toggle="table" 
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-click-to-select="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source="#tables-form-module" 
								 data-form-target="#editor-form-assets" 
								 data-form-search="#search-form-assets" 
								 data-loader-target="#table-function" 
								 data-search-querys='{"and":[
								 	{"fieldName":"type","fieldValue":"module","operator":"EQ"}
								 ]}' id="table-module">
						<thead>
							<tr>
								<th data-radio="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="name" 		data-align="center">模块名称</th>
								<th data-field="code" 		data-align="center">模块编码</th>
								<th data-field="sort" 		data-align="center">排序</th>
								<th data-field="operator" data-align="center" data-width="65" data-formatter="renderOperator" data-table-target="module">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
		
		<!-- TODO 功能列表 -->
		<div class="widget radius-bordered " id="editor-panel-function">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 功能列表 </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" data-toggle="tooltip" data-placement="top" onclick="handlerAddRow('#table-function')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor" id="tables-form-function">
					<table data-ajax="handlerSearch" 
								 data-method="post" 
								 data-toggle="table" 
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-click-to-select="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source="#tables-form-function" 
								 data-form-target="#editor-form-assets" 
								 data-form-search="#search-form-assets" 
								 data-loader-target="#table-actions" 
								 data-search-querys='{"and":[
								 	{"fieldName":"type","fieldValue":"function","operator":"EQ"}
								 ]}' id="table-function">
						<thead>
							<tr>
								<th data-radio="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="name" 		data-align="center">功能名称</th>
								<th data-field="code" 		data-align="center">功能编码</th>
								<th data-field="sort" 		data-align="center">排序</th>
								<th data-field="operator"	data-align="center" data-width="65" data-formatter="renderOperator" data-table-target="function">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
		
		<!-- TODO 操作列表 -->
		<div class="widget radius-bordered " id="editor-panel-actions">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 操作列表 </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" data-toggle="tooltip" data-placement="top" onclick="handlerAddRow('#table-actions')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor" id="tables-form-actions">
					<table data-ajax="handlerSearch" 
								 data-method="post" 
								 data-toggle="table" 
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source="#tables-form-actions" 
								 data-form-target="#editor-form-assets" 
								 data-form-search="#search-form-assets"
								 data-search-querys='{"and":[
								 	{"fieldName":"type","fieldValue":"actions","operator":"EQ"}
								 ]}' id="table-actions">
						<thead>
							<tr>
								<th data-checkbox="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="name" 		data-align="center">操作名称</th>
								<th data-field="code" 		data-align="center">操作编码</th>
								<th data-field="sort" 		data-align="center">排序</th>
								<th data-field="operator"	data-align="center" data-width="65" data-formatter="renderOperator" data-table-target="actions">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
	</div>
</div>

<!-- 查询表单 ========================================================= -->
<form id="search-form-assets" action="/miwawa/default/common/search.json" method="post" 
			name="com.app.miwawa.api.system.entity.Assets">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
</form>
<!-- 提交表单 ========================================================= -->
<form id="editor-form-assets" action="/miwawa/default/common/save" method="post" 
			name="com.app.miwawa.api.system.entity.Assets">
			<input type="hidden" name="bean['pid']" value="0" id="pid">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['systemid']" value="0" id="systemid">
			<input type="hidden" name="bean['createDate']" value="" id="createDate">
</form>

<script type="text/javascript">
<!--
	require(['domReady'],function(domReady){
		domReady(function () {
			loadData();
    });
	});
	
	function loadData(){
		var assets = jQuery("#table-system").data("selectedRow");

		require(['moment'],function(moment){
			jQuery("#createDate","#editor-form-assets").val(moment().format("YYYY-MM-DD"));
		});
		
		jQuery(".editor-body-system").loadform("load", {
			templateId: "#view-system-template",
			templatePath: "/miwawa/content/system/assets/template.html",
			templateData: assets,
			onLoadSuccess: function(){
				require(['jquery.plugin.form', 'jquery.bootstrap.editable'],function(form, editable){
					form.tooltip("[data-toggle='tooltip']");
					editable.register("[data-editable]");
				});
			}
		});

		refreshpid(assets.id);
		
		jQuery("[data-toggle='table']").bootstrapTable({
			onCheck:function(row, $element){
				if(row.id != "new") {
					jQuery("#pid", "#editor-form-assets").val(row.id);
				}
				
				var $table = jQuery(this.loaderTarget);
				
				$table.bootstrapTable("refreshOptions",{
					"searchQuerys":{
						"and":[
						 	{"fieldName":"pid","fieldValue":row.id,"operator":"EQ"}
						 ]
					}
				});
				$table.bootstrapTable("refresh");
			}
		});
		
		function refreshpid(pid){
			jQuery(":hidden[name='pid']", "#search-form-assets").val(pid);
			jQuery("#systemid", "#editor-form-assets").val(pid);
			jQuery("#pid", "#editor-form-assets").val(pid);
		}
	}
	
	function handlerAddRow(target){
		var $table = jQuery(target);
		
		switch(target){
			case "#table-function":
				var module = jQuery("#table-module").bootstrapTable("getSelections");
				if(module.length <= 0) {
					jQuery.message.warning("请选择模块！");
					return;
				}
			break;
			case "#table-actions":
				var fun = jQuery("#table-function").bootstrapTable("getSelections");
				if(fun.length <= 0) {
					jQuery.message.warning("请选择功能！");
					return;
				}
			break;
		}
		
		if(!hasEditorRow()){
			var rowdata = {
				id:'new',
        name: createInput('name', 'text', 'ext-text'),
        code: createInput('code', 'text', 'ext-text'),
        sort: createInput('sort',	'text', 'ext-text'),
        operator: createButton(target)
			};
			
			$table.bootstrapTable('insertRow', { index: 0, row: rowdata });
		}
		
		// 创建输入框
		function createInput(name, type, ext, value){
			switch(type){
				case 'text': 
					var input = "";
					if(value != undefined){
						input = '<input name="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+value+'">';
					}else{
						input = '<input name="'+name+'" type="'+type+'" class="form-control '+ext+'">';
					}
				return input;
			}
		}
		
		// 创建按钮
		function createButton(target){
			var type = target.split("-")[1];
			return ""
						+'<input name="type" type="hidden" value="'+type+'">'
						+"<span class='table-editor-tools'>"
						+"	<button class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\");'><i class='icon ion-close-circled'></i></button>"
						+"	<button class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\");'><i class='icon ion-checkmark-circled'></i></button>"
						+"</span>";
		}
		
		// 判断是否有编辑行
		function hasEditorRow(){
			return $table.bootstrapTable('getRowByUniqueId', 'new') ? true : false;
		}
	}
//-->
</script>